﻿using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace ExplorerService.Common
{
    /// <summary>
    /// Keeps track of all information regarding a client session
    /// </summary>
    public class ClientSession
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public ClientSession()
        {
            SessionOperationContexts = new List<OperationContext>();
        }

        /// <summary>
        /// The session ID
        /// </summary>
        public Guid SessionId { get; set; }

        /// <summary>
        /// Tracks the operation contexts for which this session was used
        /// </summary>
        public List<OperationContext> SessionOperationContexts { get; private set; }

        /// <summary>
        /// Terminates all Operation contexts
        /// </summary>
        public void TerminateServices()
        {
            var list = SessionOperationContexts.ToArray();
            foreach (var operationContext in list)
            {
                try
                {
                    if (operationContext != null)
                    {
                        if (operationContext.Channel != null)
                        {
                            operationContext.Channel.Close();
                        }
                    }
                }
                catch { }
                SessionOperationContexts.Clear();
            }

        }
    }
}
